package planner.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import planner.domain.User;
import planner.service.UserService;

public class LoginController extends SimpleFormController {
	private UserService userService;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		User user = (User) command;
		boolean isValid = false;
		isValid = userService.isUserValid(user);
		if(isValid) {
			if (logger.isInfoEnabled()) {
				logger.info("user is valid : " + "userId[" + user.getUserId() + "],password[" + user.getPassword() +"]");
			}
			user = userService.getUser(user);
			request.getSession().setAttribute("userSession", user);
//			return new ModelAndView(getSuccessView());
			return new ModelAndView("accoun");
		} else {
			if (logger.isInfoEnabled()) {
				logger.info("user is invalid : " + "userId[" + user.getUserId() + "],password[" + user.getPassword() +"]");
			}
			errors.reject("unValid.user");
			return super.showForm(request, response, errors);
		}
	}
}